package com.lq.demo.demo.mode.threadpool;


/**
 * 4个线程 两两对一个数进行分别进行加，减操作(有顺序要求)
 */
public class TestDemo {

    private  int j = 0;


    public static void main(String[] args) throws InterruptedException {
        TestDemo demo = new TestDemo();
        Dec decc = demo.new Dec();
        Inc incc = demo.new Inc();
        for (int i = 0; i < 2; i++) {
            Thread thread1 = new Thread(incc);
            thread1.start();
            thread1.join();
            Thread thread2 = new Thread(decc);
            thread2.start();

        }

    }

    class Inc implements Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                inc();
            }
        }
    }

    private synchronized void inc() {
        j++;
        System.out.println(" Thread(增加): " + Thread.currentThread().getName() + "  j-->" +(j));
    }


    class Dec implements Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                dec();
            }
        }
    }

    private  synchronized  void dec() {
        j--;
        System.out.println(" Thread(减少): " + Thread.currentThread().getName() + "  j-->" + (j));
    }

}
